package gui.frames;

import geo.obj.PhiObject;
import gui.panels.ObjectDrawPanel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JColorChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.SpinnerNumberModel;

import java.awt.Rectangle;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JSpinner;

public class GuiObjectConstructFrame extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private GuiMain frr;
	private JPanel btns = null;
	private JPanel opt = null;
	private ObjectDrawPanel dp = null;
	private JButton jButton = null;
	private JButton jButton1 = null;
	private boolean _editmode = false;
	private JButton jButton2 = null;
	private JLabel jLabel = null;
	private JTextField jTextField = null;
	private JButton jButton3 = null;
	private JButton colorB = null;
	private Color color = Color.BLACK;
	
	private JButton getcb(){
		if (colorB == null) {
			colorB = new JButton();
			colorB.setBounds(new Rectangle(10, 154, 130, 20));
			colorB.setText("Set Color");
			colorB.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
			        color = JColorChooser.showDialog(null, "JColorChooser Sample", color);
				}
			});
		}
		return colorB;
	}

	/**
	 * This is the default constructor
	 * @wbp.parser.constructor
	 */
	public GuiObjectConstructFrame(GuiMain fr) {
		
		super();
		frr = fr;
		_editmode = false;
		initialize();
	}
	
	public GuiObjectConstructFrame(GuiMain fr, PhiObject po) {
		
		super();
		frr = fr;
		_editmode = true;
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(500, 500);
		this.setContentPane(getJContentPane());
		this.setTitle("Object Editor");
		this.setResizable(false);
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getdp(), BorderLayout.CENTER);
			jContentPane.add(getOpt(), BorderLayout.EAST);
			jContentPane.add(getBtns(), BorderLayout.SOUTH);
			
		}
		return jContentPane;
	}
		

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getBtns() {
		if (btns == null) {
			btns = new JPanel();
			btns.setLayout(null);
			btns.setPreferredSize(new Dimension(0, 30));
			btns.add(getJButton(), null);
			btns.add(getJButton1(), null);
			
		}
		return btns;
	}

	/**
	 * This method initializes jPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getOpt() {
		if (opt == null) {
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(10, 10, 104, 15));
			jLabel.setText("Object name");
			opt = new JPanel();
			opt.setLayout(null);
			opt.setPreferredSize(new Dimension(150, 0));
			opt.add(getJButton2(), null);
			opt.add(jLabel, null);
			opt.add(getJTextField(), null);
			opt.add(getJButton3(), null);
			opt.add(getcb(), null);
		}
		return opt;
	}

	/**
	 * This method initializes jPanel2	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private ObjectDrawPanel getdp() {
		if (dp == null) {
			dp = new ObjectDrawPanel();
			dp.setLayout(null);
			dp.setPreferredSize(new Dimension(0, 20));
		}
		return dp;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setBounds(new Rectangle(10, 5, 100, 20));
			jButton.setText("OK");
			jButton.setEnabled(false);
			
			jButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if (!dp.convexState()){
						Object[] options = {"OK"};
						JOptionPane.showOptionDialog(null, "Object is concave", "Warning",
						JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE,
						null, options, options[0]);
						jButton.setEnabled(false);
					}
					else if(!dp.poleInside()){
						Object[] options = {"OK"};
						JOptionPane.showOptionDialog(null, "Pole outside object", "Warning",
						JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE, null, options, options[0]);
						jButton.setEnabled(false);
					}
					else{
						PhiObject p = new PhiObject();
						p = dp.getPhiObject();
						p.setName(getJTextField().getText());
						p.setColor(color);
						frr.addObjectToList(p);
						dispose(); //TODO
					}
				}
			});
		}
		return jButton;
	}

	/**
	 * This method initializes jButton1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setBounds(new Rectangle(120, 5, 100, 20));
			jButton1.setText("Cancel");
			jButton1.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					dispose();
				}
			});
		}
		return jButton1;
	}

	/**
	 * This method initializes jButton2	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton2() {
		if (jButton2 == null) {
			jButton2 = new JButton();
			jButton2.setBounds(new Rectangle(10, 63, 130, 20));
			jButton2.setText("Connect");
			jButton2.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					if(dp.clickstate()){
						dp.connect();
						jButton3.setEnabled(true);
						jButton2.setEnabled(false);
					}
				}
			});
			if (_editmode) jButton2.setVisible(false);
		}
		return jButton2;
	}

	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
			jTextField.setBounds(new Rectangle(10, 36, 130, 20));
			jTextField.setText("name");
		}
		return jTextField;
	}

	/**
	 * This method initializes jButton3	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton3() {
		if (jButton3 == null) {
			jButton3 = new JButton();
			jButton3.setBounds(new Rectangle(10, 100, 130, 20));
			jButton3.setText("Set pole");
			jButton3.setEnabled(false);
			jButton3.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					dp.setPole();
					jButton.setEnabled(true);
				}
			});
			
		}
		return jButton3;
	}
}
